package org.ethan.pet.controller;

import org.ethan.basic.util.AjaxResult;
import org.ethan.basic.util.PageList;
import org.ethan.pet.domain.Pet;
import org.ethan.pet.query.PetQuery;
import org.ethan.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 显示宠物的分页数据
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> pageList(@RequestBody PetQuery query){

        return  petService.queryData(query);

    }

    /**
     * 上架下架的宠物
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常！！！");
        }
    }

    /**
     * 下架上架的宠物
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常！！！");
        }
    }

    /**
     * 在宠物购买页面显示已上架的宠物，
     * @return
     */
    @PostMapping("/onsalePet")
    public PageList<Pet> onsalePet(@RequestBody PetQuery query) {

        query.setState(Pet.STATE_ONSALE);
        return petService.queryData(query);
    }

    /**
     * 根据id获取该宠物的详细信息
     * @param id id
     * @return pet
     */
    @GetMapping("/findOnsalePetById/{id}")
    public AjaxResult findOnsalePetById(@PathVariable("id") Long id) {
        try {
            Pet pet = petService.findOnsalePetById(id);
            return AjaxResult.me().setResultObj(pet);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙！！！");
        }
    }

}
